import sys,os,threading
from subprocess import *
import time
class myPopen(threading.Thread):
	def __init__(self,args):
		threading.Thread.__init__(self)
		self.proc=Popen(args,stdin=PIPE,stdout=PIPE,shell=True)
		self.quit=False

	def sendline(self,content):
		if content.startswith('quit'):
			self.quit=True
		self.proc.stdin.write(bytes(content,encoding='utf-8'))
		self.proc.stdin.flush()

	def recvline(self):
		print("deal recv...")
		line= self.proc.stdout.readline()
		print("deal recv done.")
		return str(line,encoding='utf-8')
	def dealRecv(self):
		data=self.recvline()
		if data.startswith("[image]"):
			data=data.strip('[image]').strip('\n')
			with open("ttt.png",'wb') as f:
				f.write(bytes.fromhex(data))
		else:
			print(data)

	def run(self):
		while True:
			if self.quit:
				break
			self.dealRecv()
			


if __name__=="__main__":
	proc=myPopen(os.path.join('bin','rxl'))

	proc.start()
	for line in sys.stdin:
		print("please input something\n")		
		proc.sendline(line)